class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode head = new ListNode();
        ListNode cur = head;
        int sum = 0;
        while(cur1 != null && cur2 != null){
            //这里要对新链表进行尾插
            sum += cur1.val + cur2.val;
            ListNode tmp = new ListNode(sum % 10);
            sum /= 10;
            cur.next = tmp;
            cur = cur.next;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        while(cur1 != null){
            sum += cur1.val;
            ListNode tmp = new ListNode(sum % 10);
            sum /= 10;
            cur.next = tmp;
            cur = cur.next;
            cur1 = cur1.next;
        }
        while(cur2 != null){
            sum += cur2.val;
            ListNode tmp = new ListNode(sum % 10);
            sum /= 10;
            cur.next = tmp;
            cur = cur.next;
            cur2 = cur2.next;
        }
        if(sum > 0) cur.next = new ListNode(sum);
        return head.next;
    }
}